package com.test.template.publish.config.manager;

import com.test.template.publish.config.fac.*;
import lombok.Builder;
import lombok.Data;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Builder
@Data
public class VelocityConfigManager {
    // TODO 参数验证
    private String tableName;
    private Class<?> clazzPo;
    private String controllerPrefix; // controller访问的URL的前缀，给api使用，/content/contribute
    private String controllerPackage; // controller的包名称，EX: com.elecbook.content.api
    private String servicePackageName; // service的包名称 EX: com.elecbook.content.service
    private String jsEntityPath = "@/model/"; // 根据tableName自动更新jsEntityPath，不建议直接指定
    private String jsApiPath = "@/api/null.js"; //"@/api/content/contribute/contribute.js" // 需要指定全部名称

    private Map<String, String> templateFileMap; // 模板文件的映射
    private List<Class<?>> factoryList;
    private Map<Class<?>, String> ioFileMap;// 工厂到模板文件的映射配置
    /**
     * 指定velocity的引擎文件夹后,还需要选择在此基础上寻址的文件夹
     * 例子 template文件夹下开始寻找
     */
    private String baseTemplateDirectory = "publish-manager-v2";


    // 初始化hashMap
    @PostConstruct
    public void readConfigFile() {
        // 可以后续修改为读配置文件形式
        // 内部的基础配置
        Map<String, String> templateFileMap = Stream.of(new String[][]{
                {"mp.controller", baseTemplateDirectory + "/mp.controller.vm"},
                {"mp.ServiceMapper", baseTemplateDirectory + "/mp.ServiceMapper.vm"},
                {"js.api", baseTemplateDirectory + "/js.api.vm"},
                {"vue.submit.entity", baseTemplateDirectory + "/vue.submit.entity.vm"},
                {"js.entity.class", baseTemplateDirectory + "/js.entity.class.vm"},
                {"vue.index", baseTemplateDirectory + "/vue.index.vm"},
        }).collect(Collectors.toMap(data -> data[0], data -> data[1]));

        // 初始化factoryList
        factoryList = new ArrayList<>();
        factoryList.add(ControllerConfigFactory.class);
        factoryList.add(ServiceMapperConfigFactory.class);
        factoryList.add(ApiConfigFactory.class);
        factoryList.add(JsClassConfigFactory.class);
        factoryList.add(XmlSubmitConfigFactory.class);
        factoryList.add(XmlIndexConfigFactory.class);


        // 初始化ioFileMap
        ioFileMap = new HashMap<>();
        // 配置初始化
        ioFileMap.put(ControllerConfigFactory.class, baseTemplateDirectory + "/mp.controller.vm");
        ioFileMap.put(ServiceMapperConfigFactory.class, baseTemplateDirectory + "/mp.ServiceMapper.vm");
        ioFileMap.put(ApiConfigFactory.class, baseTemplateDirectory + "/js.api.vm");
        ioFileMap.put(JsClassConfigFactory.class, baseTemplateDirectory + "/js.entity.class.vm");
        ioFileMap.put(XmlSubmitConfigFactory.class, baseTemplateDirectory + "/vue.submit.entity.vm");
        ioFileMap.put(XmlIndexConfigFactory.class, baseTemplateDirectory + "/vue.index.vm");
        // 进行配置
        setConfigs();
    }


    // 初始化工厂配置
    private void setConfigs() {
        // 添加配置
        ControllerConfigFactory.getInstance().addConfig(this);
        ServiceMapperConfigFactory.getInstance().addConfig(this);
        JsClassConfigFactory.getInstance().addConfig(this);
        ApiConfigFactory.getInstance().addConfig(this);
        XmlSubmitConfigFactory.getInstance().addConfig(this);
        XmlIndexConfigFactory.getInstance().addConfig(this);
    }


}
